/**
 * Sample C++ main entry point for testing entry point discovery.
 */

#include <iostream>
#include <string>
#include <vector>
#include <memory>

#include "core/processor.h"
#include "utils/helpers.h"
#include "api/client.h"

void print_usage(const char* program_name) {
    std::cout << "Usage: " << program_name << " [options] <input_file>" << std::endl;
    std::cout << "Options:" << std::endl;
    std::cout << "  -h, --help     Show this help message" << std::endl;
    std::cout << "  -v, --verbose  Enable verbose output" << std::endl;
    std::cout << "  -o, --output   Output file path" << std::endl;
}

int process_file(const std::string& input_path, const std::string& output_path = "") {
    try {
        // Create processor instance
        auto processor = std::make_unique<core::DataProcessor>();
        
        // Validate input
        if (!utils::validate_file(input_path)) {
            std::cerr << "Error: Invalid input file: " << input_path << std::endl;
            return 1;
        }
        
        // Process the file
        auto result = processor->process_file(input_path);
        
        if (!output_path.empty()) {
            processor->save_result(result, output_path);
            std::cout << "Results saved to: " << output_path << std::endl;
        } else {
            std::cout << "Processing completed successfully" << std::endl;
        }
        
        return 0;
        
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
}

int main(int argc, char* argv[]) {
    std::string input_file;
    std::string output_file;
    bool verbose = false;
    
    // Parse command line arguments
    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];
        
        if (arg == "-h" || arg == "--help") {
            print_usage(argv[0]);
            return 0;
        } else if (arg == "-v" || arg == "--verbose") {
            verbose = true;
        } else if (arg == "-o" || arg == "--output") {
            if (i + 1 < argc) {
                output_file = argv[++i];
            } else {
                std::cerr << "Error: --output requires a value" << std::endl;
                return 1;
            }
        } else if (arg[0] != '-') {
            input_file = arg;
        } else {
            std::cerr << "Error: Unknown option: " << arg << std::endl;
            print_usage(argv[0]);
            return 1;
        }
    }
    
    if (input_file.empty()) {
        std::cerr << "Error: Input file required" << std::endl;
        print_usage(argv[0]);
        return 1;
    }
    
    // Setup logging
    utils::setup_logging(verbose);
    
    // Process the file
    return process_file(input_file, output_file);
}